package com.zznj.xzjc.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogDownload;
import com.zznj.xzjc.ActivityAnJianDetail;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.index.FileDataAdapter;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.bean.index.FileData;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.JcjyxqResult;
import com.zznj.xzjc.net.entity.XsglxqResult;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.FileUtils;
import com.zznj.xzjc.utils.StringUtils;
import com.zznj.xzjc.utils.TimeUtils;
import com.zznj.xzjc.utils.fileutils.OkManager;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.FormBody;

/**
 * 描述：线索发现
 */
public class ActivityXsglDetail extends BaseActivity {


    Unbinder unbinder;
    @BindView(R.id.xsmc)
    TextView xsmc;
    @BindView(R.id.wfxwfsd)
    TextView wfxwfsd;
    @BindView(R.id.wfxwfssj)
    TextView wfxwfssj;

    @BindView(R.id.xsly)
    TextView xsly;
    @BindView(R.id.saje)
    TextView saje;
    @BindView(R.id.jbryj)
    TextView jbryj;


    @BindView(R.id.glajbh)
    TextView glajbh;
    @BindView(R.id.ccsj)
    TextView ccsj;
    @BindView(R.id.xgclTv)
    TextView xgclTv;
    @BindView(R.id.xsnr)
    TextView xsnr;
    @BindView(R.id.xgclRec)
    RecyclerView xgclRec;



    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_xsgl_detail_layout;
    }

    private String clueId;
    private String msgId;

    @Override
    public void initView() {
        clueId = getIntent().getExtras().getString("clueId");
        msgId = getIntent().getExtras().getString("msgId");
        setTitle("线索详情");
        initJcjy();
    }


    private FileDataAdapter xgclAdapter;
    private List<FileData> xgclList;
    private void initJcjy(){
        xgclList = new ArrayList<>();
        ((SimpleItemAnimator) xgclRec.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        xgclRec.setLayoutManager(linearLayoutManager);
        xgclAdapter = new FileDataAdapter(R.layout.gyss_item_filedata, xgclList);
        xgclRec.setAdapter(xgclAdapter);
        xgclAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                FileData fData = xgclList.get(position);
                if("jpg,jpeg,png,gif".contains(fData.getFileName().substring(fData.getFileName().lastIndexOf(".") + 1))){
                    List<String> pList = new ArrayList<>();
                    pList.add(HttpMethods.BASE_File_URL+fData.getFileId());
                    Bundle bundle = new Bundle();
                    bundle.putInt("item", 0);
                    bundle.putSerializable("listPath", (Serializable) pList);
                    RxActivityTool.skipActivity(mContext, NetPicShowActivity.class, bundle);
                }else{
                    downFile(HttpMethods.BASE_File_URL+fData.getFileId(),fData.getFileName());
                }
            }
        });
    }

    private void downFile(String downUrl,String fileName){
        File pFile = new File(FileUtils.getRootFile(),fileName);
        if (!pFile.exists()) {
            initDownDialog();
            OkManager.getInstance().downLoadFile(downUrl, fileName, new Handler(){
                @Override
                public void handleMessage(@NonNull Message msg) {
                    downDialog.dismiss();
                    if(msg.what == 100){
                        RxToast.normal("下载成功！");
                        Bundle bundle = new Bundle();
                        bundle.putString("fileName",fileName);
                        RxActivityTool.skipActivity(activity, FileDisplayActivity.class,bundle);
                    }else if(msg.what == -1){
                        RxToast.normal("下载失败！");
                        pFile.delete();
                    }
                    super.handleMessage(msg);
                }
            });
        }else{
            Bundle bundle = new Bundle();
            bundle.putString("fileName",fileName);
            RxActivityTool.skipActivity(activity, FileDisplayActivity.class,bundle);
        }
    }
    private RxDialogDownload downDialog;
    private void initDownDialog() {
        downDialog = new RxDialogDownload(this);
        downDialog.setCanceledOnTouchOutside(false);
        downDialog.getTextView().setText("下载中...");
        downDialog.show();
    }


    @Override
    protected void initData() {
        getData();
        if(!StringUtils.isEmpty(msgId)){
            readMsg();
        }
    }


    //1: 举报 2: 自首 3: 控告 4: 上级交办 5: 工作发现 6: 移送 7: 舆论监督中发现
    private HashMap<Integer, String> jbMap = new HashMap<Integer, String>() {{
        put(1, "举报");
        put(2, "自首");
        put(3, "控告");
        put(4, "上级交办");
        put(5, "工作发现");
        put(6, "移送");
        put(7, "舆论监督中发现");
    }};

    private String caseId;
    private void getData() {
        SubscriberOnNextListener<BaseBean<XsglxqResult>> onNextListener = baseBean -> {
            xsmc.setText(baseBean.getResult().getClueInfo().getClueNo());
            caseId = baseBean.getResult().getClueInfo().getCaseId();
            wfxwfsd.setText(StringUtils.toStringForWu(baseBean.getResult().getClueInfo().getAddress()));
            wfxwfssj.setText(TimeUtils.getTime(baseBean.getResult().getClueInfo().getCreateTime(),"yyyy-MM-dd"));
            xsly.setText(jbMap.get(Integer.parseInt(baseBean.getResult().getClueInfo().getClueResource())));
            saje.setText(StringUtils.isEmpty(baseBean.getResult().getClueInfo().getAllegedAmount())?"无":("￥"+StringUtils.formatMoney(Double.parseDouble(baseBean.getResult().getClueInfo().getAllegedAmount()),2)+"元"));
            jbryj.setText(StringUtils.toStringForWu(baseBean.getResult().getClueInfo().getCreateUserOpinion()));
            glajbh.setText(StringUtils.toStringForWu(baseBean.getResult().getClueInfo().getCaseNo()));

            if(StringUtils.isEmpty(baseBean.getResult().getClueInfo().getCaseNo())){
                glajbh.setText("无");
                glajbh.setTextColor(getResources().getColor(R.color.color_333333));
            }else{
                glajbh.setTextColor(getResources().getColor(R.color.title_bg));
                glajbh.setText(baseBean.getResult().getClueInfo().getCaseNo());
            }

            ccsj.setText(TimeUtils.getTime(baseBean.getResult().getClueInfo().getOccurrenceTime(),"yyyy-MM-dd"));
            xsnr.setText(StringUtils.toStringForWu(baseBean.getResult().getClueInfo().getContent()));
            if(baseBean.getResult().getClueInfo().getAttments() != null && baseBean.getResult().getClueInfo().getAttments().size() > 0){
                xgclList.addAll(baseBean.getResult().getClueInfo().getAttments());
                xgclAdapter.notifyDataSetChanged();
                xgclTv.setVisibility(View.GONE);
                xgclRec.setVisibility(View.VISIBLE);
            }else{
                xgclTv.setVisibility(View.VISIBLE);
                xgclRec.setVisibility(View.GONE);
            }
        };
        HttpMethods.getInstance().getClueDetail(new ProgressSubscriber<>(onNextListener, mContext, true), clueId);
    }

    private void readMsg() {
        SubscriberOnNextListener<BaseBean<XsglxqResult>> onNextListener = baseBean -> {

        };
        FormBody.Builder builder = new FormBody.Builder()
                .add("msgId", msgId);
        HttpMethods.getInstance().updStatus(new ProgressSubscriber<>(onNextListener, mContext, true), builder.build());
    }

    @OnClick({R.id.glajbh})
    public void onClick(View v){
        if(v.getId() == R.id.glajbh){
            if(!StringUtils.isEmpty(caseId)){
                Bundle bundle = new Bundle();
                bundle.putString("caseId", caseId);
                RxActivityTool.skipActivity(mContext, ActivityAnJianDetail.class, bundle);
            }
        }
    }
}
